package com.example.studyapp

import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.VolleyError
import com.example.studyapp.data.jsonInteraction
import com.example.studyapp.data.myRecyclerAdapter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.json.JSONObject

data class study_choice_database_send(val dataclass: String)

//选择题库
class study_choice_database : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (getSupportActionBar()!=null){//去掉标题
            getSupportActionBar()?.hide()
        };
        setContentView(R.layout.activity_study_choice_database)
        //联系远程，获取题库列表
        //val jsondata=study_choice_database_send(json_enum_DataClass.database_list.toString())
        val jsondata=study_choice_database_send("database_list")
        val jsonobj= JSONObject(Gson().toJson(jsondata))
        val choice_database_ret: Button =this.findViewById(R.id.choice_database_ret)
        //Toast.makeText(this,jsonobj.toString(),Toast.LENGTH_SHORT)
          //  .show()
        choice_database_ret.setOnClickListener {
            this.finish()
        }
        object : jsonInteraction(jsonobj){
            override fun myResposeListener(response: JSONObject) {
                init_list(response)
            }
            override fun myResposeErrorListener(err: VolleyError) {
            }
        }
    }
    //JsonArray转换成List
    private fun <T> jsonToList(jsonList: String): List<T> {
        return Gson().fromJson(jsonList, object : TypeToken<ArrayList<T>>() {}.type)
    }
    //为界面填充信息
    fun init_list(strjson: JSONObject){
        val mydata:Jstruct_database_list
        //解析数据库
        try{
            mydata=Gson().fromJson(strjson.toString(), Jstruct_database_list::class.java)
        }catch (e: Exception){
            //Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT)
              //  .show()
            return
        }
        //首先判断error是否有错误
        if(mydata.error){
            Toast.makeText(this, "未能访问到数据", Toast.LENGTH_SHORT)
                .show()
            return
        }
        if(mydata.length<=0){
            Toast.makeText(this, "数据库没有数据", Toast.LENGTH_SHORT)
                .show()
            return
        }
        val layoutManager= LinearLayoutManager(this)
        val mre: RecyclerView =this.findViewById(R.id.study_choice_database_recyclerview)
        mre.layoutManager=layoutManager
        //Toast.makeText(this, mydata.toString(), Toast.LENGTH_SHORT)
          //  .show()
        //将JSONArray转换为ArrayList
        var dblist = jsonToList<String>(mydata.listname.toString())
        //设置选项布局
        val adapter=object : myRecyclerAdapter(dblist as ArrayList<String>){
            //设置选项单击事件
            override fun onItemClick(holder: MyViewHolder, position: Int) {
                thisOnItemClick(holder, position)
            }
        }
        //Toast.makeText(this, dblist.toString(), Toast.LENGTH_SHORT)
          //  .show()
        mre.adapter=adapter
    }
    //设置点击布局
    fun thisOnItemClick(holder: myRecyclerAdapter.MyViewHolder, position: Int){
        //修改默认题库，发送题库名称给服务器
        val jstc=jstruct_change_database(
                databse = holder.title.text.toString()
        )
        var jobj:JSONObject= JSONObject(Gson().toJson(jstc))
        object :jsonInteraction(jobj){
            override fun myResposeListener(response: JSONObject) {
                //服务器应当返回对错信息
                try{
                    val result:jstruct_return_bool=Gson().fromJson(response.toString(), jstruct_return_bool::class.java)
                    if(result.error){//未能修改
                        Toast.makeText(this@study_choice_database, "未能修改题库", Toast.LENGTH_SHORT)
                            .show()
                    }else{//修改成功
                        define_home_value= holder.title.text.toString()
                        Toast.makeText(this@study_choice_database, "切换题库成功", Toast.LENGTH_SHORT)
                            .show()
                    }
                }catch (e: Exception){//提示用户修改失败
                    Toast.makeText(this@study_choice_database, "数据库连接失败", Toast.LENGTH_SHORT)
                        .show()
                }
            }
        }
    }

    //修改题库后续
    fun change_database(database: String){
        //修改主页的题库名称
        define_home(database_name = database)
    }
}